<?php
include 'config.inc.php';
$NEW_RANDOM_STRING = generateRandomString(40);
define('RANDOM_STRING', $NEW_RANDOM_STRING);
$_SESSION['random_string'] = $NEW_RANDOM_STRING;



$go 			= @$_GET['go'];
$showrec 		= @$_GET['showrec'];
$g 				= @$_GET['g'];
$t 				= @$_GET['t'];
$s 				= @$_GET['s'];
$stg 			= @$_GET['stg'];
$view 			= @$_GET['view'];
$sstg 			= @$_GET['sstg'];
$sw 			= @$_GET['sw'];
$additem 		= @$_REQUEST['additem'];
$member_error	= @$_GET['member_error'];

$swmob = intval(@$_GET['swmob']);
if (@$_SESSION['swmob'] && !$swmob){ $swmob = $_SESSION['swmob'];}
if (!@$_SESSION['swmob'] && $swmob){ $_SESSION['swmob'] = $swmob;}

if ($go == "logout"){ @$_SESSION['ss_user'] = NULL; @$_SESSION['ss_store'] = NULL; header("Location: /"); exit; }
if (isset($xcr)) if ($xcr) {
	@$_SESSION['ss_store'] = NULL;
	@$_SESSION['ss_promo'] = NULL;
}

$BGContentData 	= "";
$page_data 		= NULL;
$docenter  		= NULL;
$FooterMenu 	= "";
$onload_data  	= "";
$replace_email 	= "";
$EMAIL_DATA 	= "";

$TemplateImageA = "";
$TemplateImageB = "";
$TemplateImageC = "";
$TemplateImageJ = "";

$hideStore = intval(@$GVARS['DontShowStoreItems']);		
if (@$_SESSION['ss_user']['servid']) $hideStore = 0;

if ($hideStore && ($go == 'store')){
	$go = '';
	$g = '';
	$t = '';
	$s = '';
	$view = '';
}


if (@$GVARS['useUnicodeWebPage']) header("Content-Type: text/html; charset=utf-8");
include(SITE_ROOT . "/images/custom/template_var.php");
include(SITE_ROOT . "/includes/connection.php");
include SITE_ROOT . '/includes/classes/addhead.class.php';
include SITE_ROOT . '/includes/classes/post5.class.php';
include SITE_ROOT . '/includes/classes/checkout.class.php';

$AdditionalData = new AdditionalHead(@$GVARS['AdditionalPageContent']);

$replace_page_wid_perc = 1;
$_SESSION['ss_company'] = get_company_details();

/**
 * updated the ssl redirect
 */
$full_site_url = 'http://' . $_SESSION['ss_company']['thesite'];

if (intval(@$GVARS['SiteHasSSL'])){
    $full_site_url = 'https://' . $_SESSION['ss_company']['thesite'];
    
    if (!isset($_SERVER['HTTPS']) || !$_SERVER['HTTPS']) {
        // redirect to the https version
        $request_uri = $_SERVER['REQUEST_URI'];
        $redirect = 'Location: ' . $full_site_url . $request_uri;
        header($redirect);
    }
}

define('FULL_SITE_URL', $full_site_url);



$footer_mobile_link = '';

if (@$_SESSION['ss_company']['allowmobilesite'] && @$_SESSION['ss_company']['mobileSiteName']){
	/**
	 * Mobile site detection
	 * 
	*/
	include SITE_ROOT . '/includes/classes/Mobile_Detect.php';
	
	$mob = new Mobile_Detect;
	if ($mob->isMobile()){
		// see if they have selected to come here any way
		if (!$swmob){
			$loc = "Location: http://" . $_SESSION['ss_company']['mobileSiteName'];
			header($loc);
		}else{
			// make the footer return
			$footer_mobile_link = '<div id="footer-mobile"><p><a href="http://'.$_SESSION['ss_company']['mobileSiteName'].'">Return to Mobile site</a></p></div>';
		}
	}
 
	/* end mobile detect*/
}

$StoreMenuStyle = intval(@$GVARS['StoreMenuStyle']);

//$StoreMenuStyle = 1;

if ($StoreMenuStyle){
	$storeM = new storeMenu;	
	$store_menu = $storeM->myMenu; 
}

if (@$GVARS['ForceHomePageRedirect']==1){
	if (!$_SERVER['QUERY_STRING'] && $_SESSION['ss_company']['thesite']){
		GetFirstPage($_SESSION['ss_company']['thesite']);
	}
}elseif(@$GVARS['ForceHomePageRedirect']==2){
	if ($GVARS['ForceHomePageRedirectFrom']) {
		if ($_SERVER['QUERY_STRING'] == "g=-1&contentpage=".$GVARS['ForceHomePageRedirectFrom']){
			header("HTTP/1.1 301"); 
			header("Location: " . $full_site_url . "/");			
		}		
	}else{
		if (!$_SERVER['QUERY_STRING']){
			header("HTTP/1.1 301"); 
			header("Location: " . $full_site_url . "/a/".$GVARS['ForceHomePageRedirectTo']);
		}
	}
}

$the_temp_dir = "temp" . (int)$_SESSION['ss_company']['storedesign'];

if ($_SESSION['ss_company']['storedesign']){
	
	require_once (SITE_ROOT . "/includes/func_getexprice.php");
	require_once (SITE_ROOT . "/includes/postage.php");
	require_once (SITE_ROOT . "/includes/PostageType6.php");
	require_once (SITE_ROOT . "/includes/PostageType9.php");
	require_once (SITE_ROOT . "/includes/PostageType10.php");	
	//require_once(SITE_ROOT . "/includes/func_egate.php");
	require_once(SITE_ROOT . "/includes/functions.php");
		
	//if (intval(@$_SESSION['ss_company']['storetype']) > 1) $login_data = create_login_stuff();
	if (!@$_SESSION['ss_company']['storetype']){ $login_data = ""; $store_menu = ""; }
}

$myPageTitle = (@$page_data['title']) ?  $myPageTitle = $page_data['title'] : @$_SESSION['ss_company']['company'];
if (!$myPageTitle) $myPageTitle = "Complete Web - Grow your Business Now!";

//EchoDump($_SESSION['ss_store']);

//echo $GVARS['HideStore'];

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--[if IE 7 ]>		 <html class="no-js ie ie7 lte7 lte8 lte9" lang="en-US"> <![endif]-->
<!--[if IE 8 ]>		 <html class="no-js ie ie8 lte8 lte9" lang="en-US"> <![endif]-->
<!--[if IE 9 ]>		 <html class="no-js ie ie9 lte9>" lang="en-US"> <![endif]-->
<!--[if IE 11 ]>		 <html class="no-js ie ie8 ie9 lte9>" lang="en-US"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $myPageTitle?></title>

<meta name="description" content="<?php echo @$page_data['metacont']?>">
<meta name="keywords" content="<?php echo @$page_data['metakey']?>">

<?php if (@$page_data['MetaTagName'] && @$page_data['MetaTagData']) { ?>
<meta name="<?php echo $page_data['MetaTagName']?>" content="<?php echo $page_data['MetaTagData']?>">
<?php } ?>
<?php 
if (isset($page_data['metarobots'])) $metarobots = $page_data['metarobots']; else $metarobots = "";
echo MakeRobotsMeta($metarobots); 
?>
<?php 
if (isset($page_data['lastmodified'])) $plastmodified = $page_data['lastmodified']; else $plastmodified = "";
echo MakeLastModifiedMETA($plastmodified); 
?>
<?php if (file_exists(SITE_ROOT . '/images/custom/'.$the_temp_dir.'.css')) { echo '<link href="/images/custom/'.$the_temp_dir.'.css" rel="stylesheet" type="text/css">' . CRLF; } ?>
<link href="/includes/main.css?ver0.1" rel="stylesheet" type="text/css">
<link href="/includes/blog.css" rel="stylesheet" type="text/css">
<script src="/includes/ajax_swfobject.js" type="text/javascript"></script>
<?php if (file_exists(SITE_ROOT . '/images/favicon.ico')) {
	echo '<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />';
}else{
	echo '<link rel="shortcut icon" href="/templates/favicon.ico" type="image/x-icon" />';
}
echo CRLF;
?>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script src="/includes/modernizr.min.js" type="text/javascript"></script>
<?php
// added for ie 11

$isIE10 = (preg_match('/(?i)msie [10]/',$_SERVER['HTTP_USER_AGENT']));
$isIE11UP = (preg_match('/(?i)trident\/[7-9]/',$_SERVER['HTTP_USER_AGENT']));
$isIE10UP = ($isIE10 == 1 || $isIE11UP == 1 ? 1 : 0);
if ($isIE10UP){ ?>
<script>
$(function(){
	$('html').addClass('no-js ie ie11 lte11');
});
</script>
<?php } ?>
<link rel="stylesheet" href="/includes/menu.css" />
<?php if (!@$GVARS['NewMenuTemplate']){ ?>
<script language="JavaScript1.2" type="text/javascript" src="/images/custom/coolmenus4.js"></script>
<?php }else{ ?>
	
<script type="text/javascript" src="/includes/menucode.js"></script>
<script type="text/javascript" src="/includes/api.js"></script>
<script type="text/javascript" src="/images/custom/menuvars.js"></script>
<script type="text/javascript" src="/includes/blog.js"></script>
<script type="text/javascript" src="/includes/slider.js"></script>
<script type="text/javascript">
<?php if ($_SESSION['ss_company']['storedesign']){echo MakeTheMenus();}?>
</script>
<?php }?>
<script type="text/javascript" src="/includes/base.js"></script>
<script type="text/javascript" src="/includes/convert-gbimages.js"></script>
<script type="text/javascript" src="/includes/easybox/easybox-distrib.min.js"></script>
<link href="/includes/easybox/easybox.min.css" rel="stylesheet" />
<script type="text/javascript" src="/includes/custom.js"></script>
<?php

if (!$StoreMenuStyle){
	$TheStoreJSData = addslashes(@$store_menu);
	/*
	Now we remove the store menu and replace it with the new span data
	*/
	
	if (@$store_menu){
		$store_menu = "<span id=\"thestoremenuid\">&nbsp;</span>";
		if(!$onload_data) {$onload_data = "WriteStoreMenu();"; }else{$onload_data = $onload_data . ";WriteStoreMenu();"; }
	}
	
?>
<script language="javascript" type="text/javascript">
function WriteStoreMenu(){
	if (document.getElementById('thestoremenuid')) document.getElementById('thestoremenuid').innerHTML="<?php echo $TheStoreJSData; ?>";
}
</script>
<?php
} // this ends  the [if (!$StoreMenuStyle){] section

if (isset($page_data['AdditionalHeaderData'])) $AdditionalHeaderData = $page_data['AdditionalHeaderData']; else $AdditionalHeaderData = "";
echo $AdditionalHeaderData;

echo @ $page_data['page_css'];

?>
<?php echo $AdditionalData->getHeader();  ?>
<link href="/images/custom/store.css" rel="stylesheet" />
<script type="text/javascript">var StoreCheckoutLayout=<?php echo intval(@$GVARS['StoreCheckoutLayout']); ?>;</script>
</head>

<body onLoad="<?php echo $onload_data?>">
<?php echo $AdditionalData->getPage() ?>
<div id="cwbody" style="height: 100%">
<?php if ($_SESSION['ss_company']['storedesign'] && !@$GVARS['NewMenuTemplate']){ ?>
 <script type="text/javascript" src="/images/custom/<?php echo $the_temp_dir?>_cooldata.js"></script> 
<?php 
/*
removed on the 20/6/08 - new menu system
if ($ss_company['storedesign'] < 4){ ?>
<script type="text/javascript">
<?php echo $javascript_data?>

oCMenu.construct()
</script>
*/ ?>
<?php } ?>

<?php if ($docenter) { echo "<table cellpadding=\"0\" cellspacing=\"0\" align=\"center\" width=\"100%\"><tr><td>"; }?>
<?php 

$top_menu_items = '<script language="JavaScript1.2" type="text/javascript">CWMenu.writeMenuBar();</script>';

//$top_menu_items = buildTopMenu();

include(SITE_ROOT . "/images/custom/" . $the_temp_dir . "_main_body.php"); 
?>

<?php if ($docenter) { echo "</td></tr></table>"; }?>


<?php if (($_SESSION['ss_company']['storedesign']==4) && !@$GVARS['NewMenuTemplate']) { ?>
<script type="text/javascript">

<?php echo $javascript_data?>

oCMenu.construct()
placeElements()
oCMenu.onafterresize="placeElements()"

function placeElements(){
<?php echo $PlaceMenuJS?>

oCMenu.fromTop = pos[1]
}
</script>
<?php } ?>
<?php if (!$_SESSION['ss_company']['storedesign']){ include ("templates/blank_body.htm"); } ?>
<?php /*echo @$local_light_box;*/ ?>
<?php 

if (@$GVARS['GoogleAnalyticalCode']){
	echo stripslashes( $GVARS['GoogleAnalyticalCode'] ) ;
}else{


	if (isset($page_data['GoogleAnal'])) $do_google_anal = $page_data['GoogleAnal']; else $do_google_anal = 0;
	if ($do_google_anal){
echo "
<script type=\"text/javascript\">
var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");
document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));
</script>
<script type=\"text/javascript\">
var pageTracker = _gat._getTracker(\"".$page_data['GoogleAnal']."\");
pageTracker._initData();
pageTracker._trackPageview();
</script>";
}

}
?>


</div>

<?php
if ($go=="checkout"){
	$check = new checkoutExtra($GVARS);
	
	echo $check->makeCheck();
	echo $check->makeJscript();
}
?>


<?php echo $footer_mobile_link?>

<?php if ($StoreMenuStyle && !@$_SESSION['ss_user']['servid']) {
	 // Start Login Popup
	$username_label = 'Username';
	if (@$store->LoginLabel){
		$username_label = $store->LoginLabel;
	}
	
?>
<div id="login-popup" title="Please login" style="display: none">
	<h5><?php echo $_SESSION['ss_company']['company']?> login</h5>
	<div id="login-page" class="swapitems">	
		<p>
			<label for="username"><?php echo $username_label?>: </label>
			<input type="text" id="acc-log-username" class="ui-corner-all isreq" />
		</p>
		<p>
			<label for="password">Password: </label>
			<input type="password" id="acc-log-password" class="ui-corner-all isreq" />
		</p>
		<p>
			<label>&nbsp;</label>
			<button class="button-link" data-location="<?php echo getDataLocation(); ?>" id="logintoacc">Click to login</button>
			
		</p>		
		<p><span id="login-message">&nbsp;</span></p>
		<p><span id="forgot-mess-1"> </span> <a href="#" id="forgot-mess-2" class="show-reset"> </a>.</p>
	</div>
	<div id="passreset" style="display: none" class="swapitems" data-label="<?php echo $username_label; ?>">
		<p id="forgot-mess-3"> </p>
		<p>
			<label for="reset_username"><?php echo $username_label?>: </label>
			<input type="text" id="reset-username" class="ui-corner-all isreq" />
		</p>
		<p>
			<label>&nbsp;</label>
			<button class="button-link" id="resetpass">Reset password</button>
		</p>
		<p><span id="reset-message">&nbsp;</span></p>
		<p><span id="forgot-mess-4"> </span> <a href="#" id="forgot-mess-5" class="show-reset"> </a></p>
	</div>
	<hr />
	<p><span id="forgot-mess-6"> </span> <a href="#" id="forgot-mess-7" class="register-show"> </a></p>	
</div>

<script type="text/javascript" src="/includes/jquery.login.js?ver1.1"></script>

<?php }else{
	echo '<input type="hidden" id="accpart" style="display:none" value="'. @$NEW_RANDOM_STRING .'" />';
}  // End Login Popup ?>

<div id="confirm-popup" style="display: none" title="Confirmation"><p id="confirm-popup-mess"></p></div>
<div style="display: none" id="popup-past-order" data-inv=""><div id="po-receipt"></div></div>

<input type="hidden" id="page-check" value="<?php echo @$NEW_RANDOM_STRING; ?>" />
<script src="/includes/jquery.storeimages.js"></script>
<script src="/includes/jquery.store.js"></script>
<script>
function SearchTheStore($frm){
	var $but = $frm.find('.search-button');
	var $srch = $frm.find('.search-val');
	$.getJSON('/includes/ajax-store-search.php', {s: $srch.val(), ac: $but.attr('data-accpart')}).done(function(resp){
		if (resp.Success){
			window.location.href='/store/search/'+resp.Result;
		}
	});
}
$('.search-val').on('keypress', function(e){
	if (e.which==13) SearchTheStore($(this).parent());
});
$('.search-button').on('click', function(e){
	e.preventDefault();
	SearchTheStore($(this).parent());
});
</script>

<?php echo $AdditionalData->getFooter(); ?>
<?php
if (is_file(SITE_ROOT . '/images/custom/kw-additions.js')){
    echo '<script src="/images/custom/kw-additions.js"></script>';
}

?>
</body>
</html>
<?php

function getDataLocation(){
	$dl = @$_SERVER['REQUEST_URI'];
	$pos = strpos($dl, '/rspw/');
	if ($pos > 0){
		$x = substr($dl, 0, $pos + 1);
		return $x;
	}
	return $dl;
}


function get_company_details(){
	
	$sql = 'SELECT * FROM company LIMIT 1';
	$pdo = new pdoWrapper;
	$q = $pdo->db->prepare($sql);
	$q->execute();
	$row = $q->fetch(PDO::FETCH_ASSOC);
	
	return $row;
}

function MakeLastModifiedMETA($dts){
	if (!is_numeric($dts)) return "";
	if (!$dts) return "";
	if (strlen($dts) < 14) return "";
	$day = substr($dts, 6, 2);
	$month = substr($dts, 4, 2);
	$year = substr($dts, 0, 4);
	$hour = substr($dts, 8, 2);
	$min = substr($dts, 10, 2);
	$secs = substr($dts, 12, 2);
	$st_time = mktime($hour,$min,$secs,$month,$day, $year);
	$dt = date("D, d M Y H:i:s O", $st_time);
	return "<META http-equiv=\"Last-Modified\" content=\"".$dt."\" />\n";
}

function MakeRobotsMeta($meta){
	if (!$meta) $meta = "INDEX, FOLLOW";
	return "<meta name=\"ROBOTS\" content=\"".$meta."\">\n";	
}

function GetFirstPage($thesite){
	$sql = "SELECT `contentpagename` FROM `webdata` WHERE `grp` = '1' AND `sub1` = '0' AND `sub2` = '0' LIMIT 1";
	$pdo = new pdoWrapper;
	$sql = 'SELECT contentpagename FROM webdata WHEREgrp = 1 AND sub1 = 0 AND sub2 = 0 LIMIT 1';
	$q = $pdo->db->prepare($sql);
	$q->execute(array($XXXX));
	$row = $q->fetch(PDO::FETCH_ASSOC);	
	if ($q->rowCount() > 0){
		$contentpagename = trim($row['contentpagename']);
		if ($contentpagename != ''){
			$url = 'http:';
			if (intval($GVARS['SiteHasSSL'])){
				$url = 'https:';
			}
			header("Location: " . $url . "//" . $thesite . "/a/" . str_replace(" ", "_", $contentpagename) );
		}
	}
}

function MakeLightBoxScript(){
	/**/
	$ret = "<script src=\"/includes/lightbox.js\"></script>\n";
	$ret .= "<script>\n";
	$ret .= "  $(function($) {\n";
	$ret .= "      $('a').smoothScroll({\n";
	$ret .= "        speed: 1000,\n";
	$ret .= "        easing: 'easeInOutCubic'\n";
	$ret .= "      });\n";
	$ret .= "\n";
	$ret .= "      $('.showOlderChanges').on('click', function(e){\n";
	$ret .= "        $('.changelog .old').slideDown('slow');\n";
	$ret .= "        $(this).fadeOut();\n";
	$ret .= "        e.preventDefault();\n";
	$ret .= "      })\n";
	$ret .= "  });  \n";
	$ret .= "</script>\n";
	return $ret; 
}


?>
